// pages/home/progressTask/index.ts
import api from '../../../api/api'

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    taskList: [],
    item: {},
    btnType: '',
    outputVisible: false,
    outputValue: "0",
    image: 'https://tdesign.gtimg.com/miniprogram/images/empty1.png',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 查询未完成的工序任务
    async findTaskList() {
      let params = { state: 2 };
      let _data = [];
      const res: any = await api.findTaskList(params);
      if (res && res.data && res.data.length > 0) {
        _data = res.data.map((item: any) => {
          let { equipment } = item
          return { ...item, equipmentNumber: equipment }
        })
      }
      if (_data.length > 0) {
        this.setData({ taskList: _data.sort((a: any, b: any) => new Date(b['createTime']) - new Date(a['createTime'])) })
      }
    },

    goDetails(e: any) {
      const { obj } = e.currentTarget.dataset;
      wx.navigateTo({
        url: '/pages/details/index',
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: obj })
        }
      })
    },
    // 上机按钮
    async upplaneBtn(e: any) {
      let { item } = e.currentTarget.dataset;
      let { id, techName, taskNum, equipmentNumber, partDrawingNumber } = item;
      let params = {
        techniqueId: id, operated: "1", techName, partDrawingNumber, equipmentNumber, taskNum
      };
      const res: any = await api.upplane(params);
      if (res.code == 0) {
        wx.showToast({
          title: res.data,
          icon: "none",
          duration: 2000,
        });
        // 刷新未完成的加工任务
        this.findTaskList();
      } else {
        wx.showToast({
          title: res.data,
          icon: "none",
          duration: 2000,
        });
      }
    },
    // 下机按钮
    deplaneBtn(e?: any) {
      // 如果e有值,说明是点击的下机按钮, 没有值则说明是完成按钮在调用下机按钮
      if (e) {
        let { item, type } = e.currentTarget.dataset;
        // 产出弹框打开
        this.setData({ outputVisible: true, item, btnType: type })
      } else {
        let { item, btnType } = this.data;
        // 产出弹框打开
        this.setData({ outputVisible: true, item, btnType })
      }

    },

    // 完成按钮
    async completeBtn(e: any) {
      let { item, type } = e.currentTarget.dataset;
      /* 
        1.判断处于上/下机状态
        2.如果当前处于上机状态, 需要先执行下机操作, 在执行完成操作
      */
      // 1. 判断处于上/下机状态
      let { isUsed, techName, taskNum, equipmentNumber } = item;
      this.setData({ item, btnType: type })
      // 如果是下机 isUsed == 1, 调用下机操作
      if (parseInt(isUsed) == 1) {
        // 调用下机操作
        this.deplaneBtn();
      } else {
        let params = { techName, taskNum, equipmentNumber };
        const res: any = await api.executeState(params);
        if (res.code == 0) {
          wx.showToast({
            title: res.data,
            icon: "none",
            duration: 2000,
          });
          this.findTaskList();
        } else {
          wx.showToast({
            title: res.data,
            icon: "none",
            duration: 2000,
          });
        }
      }
    },

    // 产出弹框确认按钮
    async outputConfirm(e: any) {
      let outputValue: string = this.data.outputValue
      if (outputValue.toString() == "" || parseInt(outputValue) < 0) {
        wx.showToast({
          title: "产出数量需大于或等于0！",
          icon: "none",
          duration: 2000,
        });
        return;
      }
      // 点击产出弹框确认按钮后会执行下机操作
      let item: any = this.data.item;
      let btnType: string = this.data.btnType;
      let params = { techniqueId: item.id, techName: item.techName, taskNum: item.taskNum, equipmentNumber: item.equipment, partDrawingNumber: item.partDrawingNumber, output: parseInt(outputValue), operated: "0" };
      const res: any = await api.deplane(params);
      if (res.code == 0) {
        wx.showToast({
          title: res.data,
          icon: "none",
          duration: 2000,
        });
        this.setData({ outputVisible: false });
        // 如果点击的是完成按钮, 需要执行完成操作
        if (btnType == 'complete') {
          let { techName, taskNum, equipmentNumber } = item
          let params = { techName, taskNum, equipmentNumber };
          const res: any = await api.executeState(params);
          if (res.code == 0) {
            wx.showToast({
              title: res.data,
              icon: "none",
              duration: 2000,
            });
          } else {
            wx.showToast({
              title: res.data,
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          wx.showToast({
            title: res.data,
            icon: "none",
            duration: 2000,
          });
        }
      }
      this.findTaskList();
    },
    // 产出弹框取消按钮
    outputCancel() {
      this.setData({ outputVisible: false })
    },



  },

  // 组件所在页面的生命周期
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.findTaskList();
    }
  }
})